﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace JMT.Data
{
    public class JMSolution : IJModel, IJContainer<JClass>, IJContainer<JDiagram>, IJContainer<JPackage>, IJContainer<JInterface>, IJContainer<JRelation>
    {
        private JContainer<JClass> _jClassContainer;
        private JContainer<JDiagram> _jDiagramContainer;
        private JContainer<JPackage> _jPackageContainer;
        private JContainer<JInterface> _jInterfaceContainer;
        private JContainer<JRelation> _jRelationContainer;

        public JMSolution()
            : base(JConstString.TSolution)
        {
            _jClassContainer = new JContainer<JClass>();
            _jDiagramContainer = new JContainer<JDiagram>();
            _jPackageContainer = new JContainer<JPackage>();
            _jInterfaceContainer = new JContainer<JInterface>();
            _jRelationContainer = new JContainer<JRelation>();
        }

        #region class container
        public void Add(JClass jModel)
        {
            _jClassContainer.Add(jModel);
            jModel.Container = this;
        }

        public void Remove(JClass jModel)
        {
            jModel.Container = null;
            _jClassContainer.Remove(jModel);
        }

        JClass IJContainer<JClass>.Get(Guid id)
        {
            return _jClassContainer.Get(id);
        }

        List<JClass> IJContainer<JClass>.ToList()
        {
            return _jClassContainer.ToList();
        }
        
        public void Register(JContainerUpdate<JClass> update)
        {
            _jClassContainer.Register(update);
        }
        #endregion

        #region diagram container
        public void Add(JDiagram jModel)
        {
            _jDiagramContainer.Add(jModel);
            jModel.Container = this;
        }

        public void Remove(JDiagram jModel)
        {
            jModel.Container = null;
            _jDiagramContainer.Remove(jModel);
        }

        JDiagram IJContainer<JDiagram>.Get(Guid id)
        {
            return _jDiagramContainer.Get(id);
        }

        List<JDiagram> IJContainer<JDiagram>.ToList()
        {
            return _jDiagramContainer.ToList();
        }

        public void Register(JContainerUpdate<JDiagram> update)
        {
            _jDiagramContainer.Register(update);
        }
        #endregion

        #region package container
        public void Add(JPackage jModel)
        {
            _jPackageContainer.Add(jModel);
            jModel.Container = this;
        }

        public void Remove(JPackage jModel)
        {
            jModel.Container = null;
            _jPackageContainer.Remove(jModel);
        }

        JPackage IJContainer<JPackage>.Get(Guid id)
        {
            return _jPackageContainer.Get(id);
        }

        List<JPackage> IJContainer<JPackage>.ToList()
        {
            return _jPackageContainer.ToList();
        }

        public void Register(JContainerUpdate<JPackage> update)
        {
            _jPackageContainer.Register(update);
        }
        #endregion

        #region interface container
        public void Add(JInterface jModel)
        {
            _jInterfaceContainer.Add(jModel);
            jModel.Container = this;
        }

        public void Remove(JInterface jModel)
        {
            jModel.Container = null;
            _jInterfaceContainer.Remove(jModel);
        }

        JInterface IJContainer<JInterface>.Get(Guid id)
        {
            return _jInterfaceContainer.Get(id);
        }

        List<JInterface> IJContainer<JInterface>.ToList()
        {
            return _jInterfaceContainer.ToList();
        }

        public void Register(JContainerUpdate<JInterface> update)
        {
            _jInterfaceContainer.Register(update);
        }
        #endregion

        #region relation container
        public void Add(JRelation jModel)
        {
            _jRelationContainer.Add(jModel);
        }

        public void Remove(JRelation jModel)
        {
            _jRelationContainer.Remove(jModel);
        }

        JRelation IJContainer<JRelation>.Get(Guid id)
        {
            return _jRelationContainer.Get(id);
        }

        List<JRelation> IJContainer<JRelation>.ToList()
        {
            return _jRelationContainer.ToList();
        }

        public void Register(JContainerUpdate<JRelation> update)
        {
            _jRelationContainer.Register(update);
        }
        #endregion

        public IJModel GetModel(Guid id)
        {
            IJModel bingoClass = null;
            List<JClass> classList = _jClassContainer.ToList();
            foreach (var jClass in classList)
            {
                if (jClass.Id == id)
                {
                    bingoClass = jClass;
                    break;
                }
            }

            if (bingoClass == null)
            {
                List<JInterface> interfaceList = _jInterfaceContainer.ToList();
                foreach (var jInterface in interfaceList)
                {
                    if (jInterface.Id == id)
                    {
                        bingoClass = jInterface;
                        break;
                    }
                }
            }

            if (bingoClass == null)
            {
                List<JPackage> packageList = _jPackageContainer.ToList();
                foreach (var jPackage in packageList)
                {
                    bingoClass = jPackage.GetModel(id);
                    if (bingoClass != null)
                    {
                        break;
                    }
                }
            }
            return bingoClass;
        }
    }
}
